// 初始化雷场
function initmapMine() {
  switch (degree) {
    case 'simple':
      mapMine.mapWidth = 9;
      mapMine.mapHeight = 9;
      mapMine.mapMine = 10;
      mapMine.mapFlag = 10;
      break;
    case 'normal':
      mapMine.mapWidth = 16;
      mapMine.mapHeight = 16;
      mapMine.mapMine = 40;
      mapMine.mapFlag = 40;
      break;
    case 'difficult':
      mapMine.mapWidth = 30;
      mapMine.mapHeight = 16;
      mapMine.mapMine = 99;
      mapMine.mapFlag = 99;
      break;
  }
  firstClick = true;
  state = true;
  // 初始化环境
  wrapper.style.width = mapMine.mapWidth * 30 + 2 + 'px';
  wrapper.style.height = mapMine.mapHeight * 30 + 52 + 'px';
  numFlag.innerHTML = mapMine.mapFlag;
  numTime.innerHTML = 0;
  btnGame.innerHTML = addIcon('xiaolian');
  clearInterval(timer);
  // 添加雷场块
  mapMine.map = [];
  mineField.innerHTML = null;
  for (var i = 1; i <= mapMine.mapHeight; i++) {
    mapMine.map[i] = [];
    for (var j = 1; j <= mapMine.mapWidth; j++) {
      mapMine.map[i][j] = 0;
      mineField.appendChild(document.createElement('div'));
    }
  }
  for (i = 1; i <= mapMine.mapFlag; i++) {
    createMine();
  }
  console.clear();
  console.log('游戏开始，当前难度：' + degree);
}

// 生成地雷
function createMine() {
  var mineWidth = Math.ceil(Math.random() * mapMine.mapWidth);
  var mineHeight = Math.ceil(Math.random() * mapMine.mapHeight);
  mapMine.map[mineHeight][mineWidth] == 1 ? createMine() : mapMine.map[mineHeight][mineWidth] = 1;
}

// 扩散检索
function spreadBfs(coord) {
  for (var i = -1; i <= 1; i++) {
    for (var j = -1; j <= 1; j++) {
      var x = coord.x + i;
      var y = coord.y + j;
      if (x > 0 && x <= mapMine.mapHeight && y > 0 && y <= mapMine.mapWidth) {
        var currDiv = div[(x-1)*mapMine.mapWidth+y-1];
        mapMine.map[coord.x][coord.y] = 4;
        currDiv.style.backgroundColor = '#fff';
        var check = checkClick({x,y});
        if (check != 0) {
          currDiv.innerHTML = check;
          currDiv.style.color = numColor[check];
        }else{
          if(mapMine.map[x][y] == 0)
            spreadBfs({x,y});
        }
        mapMine.map[x][y] = 4;
      }
    }
  }
}

// 检查点击周围
function checkClick(coord) {
  var num = 0;
  for (var i = -1; i <= 1; i++) {
    for (var j = -1; j <= 1; j++) {
      var x = coord.x + i;
      var y = coord.y + j;
      if (x > 0 && x <= mapMine.mapHeight && y > 0 && y <= mapMine.mapWidth) {
        if (mapMine.map[x][y] == 1 || mapMine.map[x][y] == 3)
          num++;
      }
    }
  }
  return num;
}


// 刷新地图
function reMap() {
  // for()
}

// iconfont
function addIcon(icon) {
  return '<i class="iconfont icon-' + icon + '"></i>';
}

// 判断结果
function judgeMine(){
  var num = 0;
  for(var i = 1; i <= mapMine.mapHeight; i++){
    for(var j = 1; j <= mapMine.mapWidth; j++){
      if(mapMine.map[i][j] >= 2 )
        num++;
    }
  }
  if(num == mapMine.mapWidth*mapMine.mapHeight){
    state = false;
    clearInterval(timer);
    str = '恭喜你完成扫雷，用时' + numTime.innerHTML + '秒';
    alert(str);
  }
}

// 结束游戏
function gameOver(){
  console.log('Game Over');
  state = false;
  clearInterval(timer);
  btnGame.innerHTML = addIcon('kulian');
  for(var i = 1; i <= mapMine.mapHeight; i++){
    for(var j = 1; j <= mapMine.mapWidth; j++){
      if(mapMine.map[i][j] == 1 || mapMine.map[i][j] == 3){
        var currDiv = div[(i-1)*mapMine.mapWidth+j-1];
        currDiv.innerHTML = addIcon('dilei');
        currDiv.style.color = '#333';
      }
    }
  }
}